<template>
	<view>
		<view v-for="(item,i) in studata.goods" :key="i" @tap="Tapcidsub(item)">
				<qi-goods :goods="item"></qi-goods>
		</view>
	</view>
</template>

<script setup>
	import { reactive, onMounted, getCurrentInstance } from 'vue';
	import {onLoad,onReachBottom} from '@dcloudio/uni-app'
	const studata = reactive({
	goods:[],
	queryObj: {
		query: '',
		cid: '',
		pagenum: 1,
		pagesize: 10
	},
	total: 0,
	// 节流阀
	isloading: false
	});
	
	onLoad((option) => {
		console.log(option);
				studata.queryObj.cid = option.cid || ''
	  studata.queryObj.query = option.query || ''
	GetListsearch()
				});
	const { proxy } = getCurrentInstance();
	let GetListsearch = (val) =>{
		// 打开节流阀
		studata.isloading = true;
	
	proxy.$http({ url: '/goods/search',data: studata.queryObj}).then(res => {
		if (res.meta.status === 200) {
	// 关闭节流阀
	studata.isloading = false;
	studata.goods = [...studata.goods, ...res.message.goods];
	studata.total = res.message.total;
	console.log(studata.goods);
		} else{
				showToast('错误');
				
		}
	});
	};
	const Tapcidsub = (id) =>{
		console.log(id);
		uni.navigateTo({
			url:'/subpages/goods_detail/goods_detail?goods_id=' + id.goods_id
		  })
	};
	onReachBottom( () =>{
			if (studata.queryObj.pagenum * studata.queryObj.pagesize >= studata.total)
	{ return showToast('没了')}
			if (studata.isloading) return;
			// 让页码值自增+1
			studata.queryObj.pagenum++;
			GetListsearch();
	}) 
	/* 提示框 */
	let showToast = (title = '', icon = 'none', duration = 1500) => {
		uni.showToast({
			title,
			icon,
			duration,
			position: 'center',
			mask: true
		});
	}
</script>

<style lang="scss">

</style>
